﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;

using BusinessManagementStudio.Models.BusinessLogic;
using BusinessManagementStudio.Models.BusinessObject.Entities;
using BusinessManagementStudio.Models.BusinessObject.Services;
using BusinessManagementStudio.ViewModel;
using BusinessManagementStudio.Utilities;

namespace BusinessManagementStudio.Controllers
{
    public class OrderController : Controller
    {
        private CompanyService cs = new CompanyService();
        private CompanyOrder comOrder = new CompanyOrder();
        private FMService fms = new FMService();
        private List<Order> tempOrderList = new List<Order>();
        private OrderService os = new OrderService();
        private User user = new User();

        //
        // GET: /Order/

        public ActionResult Index()
        {
            Company company = new Company();
            company.CompanyNameList = cs.GetCompanyNameList();

            //if (TempData["UserObj"] != null)
            //    user = (User)TempData["UserObj"];

            return View(company);
        }

        //
        // GET: /Controller/MyAction/{id}
        [HttpPost]
        public ActionResult Switch(FormCollection collection)
        {
            String ViewName = "JLLPOOrder";
            var selectedValue = collection["Company"];

            if (selectedValue == "7")
                ViewName = "JLLORDEROrder";

            if (TempData["UserObj"] != null)
                user = (User)TempData["UserObj"];

            comOrder.ViewName = ViewName;
            comOrder.IsUploaded = false;
            comOrder.fm = new FM();
            comOrder.fm.FMNameList = fms.GetFMNameList();
            comOrder.CreateDate = DateTime.Now;
            comOrder.NextAvaliableNumber = os.GetNextAvailableQuotationNo(selectedValue).ToString("0000");
            comOrder.company = cs.GetCompanyByCompanyID(int.Parse(selectedValue));
            comOrder.company.CompanyNameList = cs.GetCompanyNameList();
            comOrder.CurrentUser = user;            

            TempData["UserObj"] = user;
            TempData["CurrentViewName"] = ViewName;
            //if (selectedValue == "7")
            //    TempData["CompanyOrderForComp7"] = comOrder;
            //else if (selectedValue == "3")
            TempData["CompanyOrder"] = comOrder;

            return View(comOrder.ViewName, comOrder);
        }

        [HttpPost]
        public ActionResult MultipleCommand(String command, HttpPostedFileBase file, FormCollection collection)
        {
            if (command == "Upload Excel")
                return UploadExcel(file);
            else if (command == "Cancel")
                return RedirectToAction("Index");
            else if (command == "Clear")
                return Clear();
            else if (command == "Confirm")
                return Confirm();
            else if (command == "Add")
                return Add(collection);
            else if (command == "BackToMainMenu")
            {
                //if (TempData["CompanyOrder"] != null)
                //{
                //    comOrder = (CompanyOrder)TempData["CompanyOrder"];

                //    //if (TempData["CurrentViewName"] == "JLLPOOrder")
                //    //    comOrder = (CompanyOrder)TempData["CompanyOrderForComp3"];

                //    if (comOrder.CurrentUser.UserType == "Administrator")
                //        return RedirectToAction("Admin", "User");
                //    else if (comOrder.CurrentUser.UserType == "User")
                //        return RedirectToAction("NonAdmin", "User");
                //}
                //else if (TempData["UserObj"] != null)
                //{
                //    user = (User)TempData["UserObj"];

                //    if (user.UserType == "Administrator")
                //        return RedirectToAction("Admin", "User");
                //    else if (user.UserType == "User")
                //        return RedirectToAction("NonAdmin", "User");
                //}
                return RedirectToAction("Admin", "User");
            }

            return View();
        }

        [HttpGet]
        public ActionResult Add(FormCollection collection)
        {
            List<Order> JLLPOList = new List<Order>();

            if (TempData["CompanyOrder"] != null)
                comOrder = (CompanyOrder)TempData["CompanyOrder"];

            if (comOrder.orderList != null)
                JLLPOList = comOrder.orderList;
   
            var selectedValueFMCode = collection["FMCode"];
            String CurrentQuotationNo = comOrder.NextAvaliableNumber;
            var location = collection["Location"];

            Order tempJLLPO = new Order();
            tempJLLPO.FMCode = selectedValueFMCode;
            tempJLLPO.QuotationNo = CurrentQuotationNo;
            tempJLLPO.Location = location;
            tempJLLPO.CreationDate = DateTime.Now;
            tempJLLPO.CompanyType = "3";
            tempJLLPO.CompanyName = "JLL - (PO)";

            JLLPOList.Add(tempJLLPO);

            comOrder.orderList = JLLPOList;
            int tempNum = int.Parse(CurrentQuotationNo) + 1;
            comOrder.NextAvaliableNumber = tempNum.ToString("0000");
            comOrder.company = new Company();
            comOrder.company.CompanyNameList = cs.GetCompanyNameList();

            TempData["CompanyOrder"] = comOrder;

            return View(comOrder.ViewName, comOrder);
        }

        [HttpGet]
        public ActionResult Confirm()
        {
            if (TempData["CompanyOrder"] != null)
            {
                comOrder = (CompanyOrder)TempData["CompanyOrder"];
                //if (TempData["CurrentViewName"] == "JLLPOOrder")
                //    comOrder = (CompanyOrder)TempData["CompanyOrderForComp3"];

                comOrder.IsUploaded = true;

                //if (comOrder != null && TempData["CurrentViewName"] == "JLLORDEROrder")
                //    os.CreateOrders(comOrder.JLLOrderList);
                //else if (comOrder != null && TempData["CurrentViewName"] == "JLLPOOrder")
                    os.CreateOrders(comOrder.orderList);

                if (TempData["UserObj"] != null)
                    comOrder.CurrentUser = (User)TempData["UserObj"];
            }

            return View("OrderConfirmation", comOrder);
        }

        [HttpGet]
        public ActionResult Clear()
        {
            if (TempData["CompanyOrder"] != null)
            {
                comOrder = (CompanyOrder)TempData["CompanyOrder"];

                //if (TempData["CompanyOrderForComp3"] != null)
                //{
                //    comOrder = (CompanyOrder)TempData["CompanyOrderForComp3"];
                //    comOrder.NextAvaliableNumber = os.GetNextAvailableQuotationNo("3").ToString("0000");
                //}

                //if (comOrder.ViewName == "JLLORDEROrder")
                //    comOrder.orderList = null;
                //else if (comOrder.ViewName == "JLLPOOrder")
                //{
                //    comOrder.JLLPOList = null;
                //    TempData["CompanyOrderForComp3"] = comOrder;
                //}
                comOrder.orderList = null;
            }

            return View(comOrder.ViewName, comOrder);
        }

        [HttpPost]
        public ActionResult UploadExcel(HttpPostedFileBase file)
        {
            String newPath = Configuration.JLLOrderPath + file.FileName;

            comOrder.ViewName = "JLLORDEROrder";
            // Get All orders from the database
            List<Order> orderListFromDB = os.GetSpecificCompanyOrders("7");
            List<Order> orderListFromExcel = ExcelOpenXmlUtil.GetAllJLLOrderRecordsFromFile(newPath, "JLLOrder", 2);

            comOrder.orderList = orderListFromExcel.Except(orderListFromDB, new ServiceNoComparator()).ToList();
            tempOrderList = orderListFromExcel.Except(orderListFromDB, new ServiceNoComparator()).ToList();
            comOrder.company = new Company();
            comOrder.company.CompanyNameList = cs.GetCompanyNameList();

            TempData["CompanyOrder"] = comOrder;

            return View(comOrder.ViewName, comOrder);
        }

    }
}
